package com.ssish.saas.eccs.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;





















import com.ssish.saas.eccs.mapper.ClaimsMapper;
import com.ssish.saas.eccs.mapper.ClaimsVoMapper;
import com.ssish.saas.eccs.model.Claims;



@Service
public class AuthorizationService {
	
	
	
	 
	 @Autowired
	 private ClaimsVoMapper claimsMapper;
	 @Autowired
	 private ClaimsMapper  claimsMapperDao;
	 
	 public List<Map<String,Object>> queryAuthorInfoList(Map<String,Object> map)throws Exception{
		 List<Map<String,Object>> list=claimsMapper.queryAuthorInfoList( map);
		 List<Map<String,Object>> claimslist=new ArrayList<Map<String,Object>>();
		 if(list!=null&&list.size()>0){
			for (Map<String, Object> claimsmap : list) {
			  String  claimsStatus=(String) claimsmap.get("claimsStatus");
			  System.out.println("==claimsStatus=="+claimsStatus);
			  String  lastTransactionStatus=(String) claimsmap.get("lastTransactionStatus");
			  if("04".equals(lastTransactionStatus)&&("10".equals(claimsStatus)||"11".equals(claimsStatus))){
				  claimsmap.put("claimsStatusName", "补件");
			  }else if("14".equals(lastTransactionStatus)&&("10".equals(claimsStatus)||"11".equals(claimsStatus))){
				  claimsmap.put("claimsStatusName", "报批保险人");
			  }else if("10".equals(claimsStatus)){
				  claimsmap.put("claimsStatusName", "申请授权");
			  }else if("11".equals(claimsStatus)){
				  claimsmap.put("claimsStatusName", "等待授权批准");
			  }else if("12".equals(claimsStatus)){
				  claimsmap.put("claimsStatusName", "批准授权 ");
			  }else if("13".equals(claimsStatus)){
				  claimsmap.put("claimsStatusName", "拒绝授权");
			  }
			  claimslist.add(claimsmap);
		   }
		 }
		 
		 return  claimslist;
	 };
	 
	 
	 public Map<String,Object> queryAuthorDetilById(Integer claimsId){
		 List<Map<String,Object>>  claimsList=claimsMapper.queryAuthorDetilById(claimsId);
		 Map<String,Object> claimsMap=null;
		 if(claimsList!=null&&claimsList.size()>0){
			 claimsMap= claimsList.get(0);
		 }
		 return claimsMap;
	 }
	 
	 
	 public void exportData(Map<String,Object> map,HttpServletResponse respones) throws Exception{
		 List<Map<String,Object>> list=claimsMapper.queryAuthorInfoList( map);
		 List<Map<String,Object>> claimslist=new ArrayList<Map<String,Object>>();
		 if(list!=null&&list.size()>0){
				for (Map<String, Object> claimsmap : list) {
				  String  claimsStatus=(String) claimsmap.get("claimsStatus");
				  System.out.println("==claimsStatus=="+claimsStatus);
				  String  lastTransactionStatus=(String) claimsmap.get("lastTransactionStatus");
				  if("04".equals(lastTransactionStatus)&&("10".equals(claimsStatus)||"11".equals(claimsStatus))){
					  claimsmap.put("claimsStatusName", "补件");
				  }else if("14".equals(lastTransactionStatus)&&("10".equals(claimsStatus)||"11".equals(claimsStatus))){
					  claimsmap.put("claimsStatusName", "报批保险人");
				  }else if("10".equals(claimsStatus)){
					  claimsmap.put("claimsStatusName", "申请授权");
				  }else if("11".equals(claimsStatus)){
					  claimsmap.put("claimsStatusName", "等待授权批准");
				  }else if("12".equals(claimsStatus)){
					  claimsmap.put("claimsStatusName", "批准授权 ");
				  }else if("13".equals(claimsStatus)){
					  claimsmap.put("claimsStatusName", "拒绝授权");
				  }
				  claimslist.add(claimsmap);
			   }
			 }
			  HSSFWorkbook wb = new HSSFWorkbook();
	          HSSFSheet sheet = wb.createSheet("事先授权");
	          sheet.setColumnWidth(0, 6000);
	          sheet.setColumnWidth(1, 6000);
	          sheet.setColumnWidth(2, 6000);
	          sheet.setColumnWidth(3, 6000);
	          sheet.setColumnWidth(4, 6000);
	          sheet.setColumnWidth(5, 6000);
	          sheet.setColumnWidth(6, 6000);
	          sheet.setColumnWidth(7, 6000);
	          sheet.setColumnWidth(8, 6000);
	          sheet.setColumnWidth(9, 6000);
	          sheet.setColumnWidth(10, 6000);
	          sheet.setColumnWidth(11, 6000);
	          sheet.setColumnWidth(12, 6000);
	          sheet.setColumnWidth(13, 6000);
	          // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
	          HSSFRow row = sheet.createRow((int) 0);
	          // 第四步，创建单元格，并设置值表头 设置表头居中
	          HSSFCellStyle style = wb.createCellStyle();
	          style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
	          HSSFCell cell = row.createCell(0);
	          cell.setCellValue("授权编号");
	          cell.setCellStyle(style);
	          cell = row.createCell(1);
	          cell.setCellValue("姓名");
	          cell.setCellStyle(style);
	          cell = row.createCell(2);
	          cell.setCellValue("保险卡号");
	          cell.setCellStyle(style);
	          cell = row.createCell(3);
	          cell.setCellValue("证件号");
	          cell.setCellStyle(style);
	          cell = row.createCell(4);
	          cell.setCellValue("生日");
	          cell.setCellStyle(style);
	          cell = row.createCell(5);
	          cell.setCellValue("预约就诊日期");
	          cell.setCellStyle(style);
	          cell = row.createCell(6);
	          cell.setCellValue("预估账单金额");
	          cell.setCellStyle(style);
	          cell = row.createCell(7);
	          cell.setCellValue("预估天数");
	          cell.setCellStyle(style);
	          cell = row.createCell(8);
	          cell.setCellValue("授权状态");
	          cell.setCellStyle(style);
	          cell = row.createCell(9);
	          cell.setCellValue("备注");
	          cell.setCellStyle(style);
	          cell = row.createCell(10);
	          cell.setCellValue("保险人");
	          cell.setCellStyle(style);
	          cell = row.createCell(11);
	          cell.setCellValue("保险责任");
	          cell.setCellStyle(style);
	         for(int i=0;i<claimslist.size();i++){
	        	 Map<String,Object> dataMap=claimslist.get(i);
	        	  row = sheet.createRow(i + 1);
	              HSSFCell cellContent = null;
	              cellContent = row.createCell(0);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("claimsId")==null?"":dataMap.get("claimsId")+"");

	              cellContent = row.createCell(1);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("patientName")==null?"":dataMap.get("patientName")+"");

	              cellContent = row.createCell(2);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("cardNo")==null?"":dataMap.get("cardNo")+"");

	              cellContent = row.createCell(3);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("newIc")==null?"":dataMap.get("newIc")+"");

	              cellContent = row.createCell(4);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("birthDate")==null?"":dataMap.get("birthDate")+"");

	              cellContent = row.createCell(5);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("admissionDate")==null?"":dataMap.get("admissionDate")+"");

	              cellContent = row.createCell(6);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("estBills")==null?"":dataMap.get("estBills")+"");

	              cellContent = row.createCell(7);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("estLos")==null?"":dataMap.get("estLos")+"");

	              cellContent = row.createCell(8);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("claimsStatusName")==null?"":dataMap.get("claimsStatusName")+"");

	              cellContent = row.createCell(9);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("payRemarks")==null?"":dataMap.get("payRemarks")+"");

	              cellContent = row.createCell(10);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("employeeName")==null?"":dataMap.get("employeeName")+"");

	              cellContent = row.createCell(11);
	              cellContent.setCellStyle(style);
	              cellContent.setCellValue(dataMap.get("coverageId")==null?"":dataMap.get("coverageId")+"");
	         }
	         OutputStream fOut = null;
	          ByteArrayOutputStream out = null;
	          try {
	              respones.setContentType("application/vnd.ms-excel");
	              respones.setHeader("content-disposition", "attachment;filename=" + "authorization" + ".xls");
	              fOut = respones.getOutputStream();
	              out = new ByteArrayOutputStream();
	              wb.write(out);
	              fOut.write(out.toByteArray());
	          } catch (Exception e) {
	              e.printStackTrace();
	              throw e;
	          } finally {
	              out.close();
	              fOut.flush();
	              fOut.close();
	          }
		 
		 
	 }
	 
	 
	 public Claims selectClaimsById(Integer claimsId) {
		return claimsMapperDao.selectByPrimaryKey(claimsId);
	 }


	/*public String getPrintUrl(Map<String, Object> map) {
		  String cid=map.get("cid")+"";
    	  String trxdt=map.get("trxdt")+"";
    	  String csts=map.get("csts")+"";
    	  String usid=map.get("usid")+"";
    	  String docCD=map.get("docCD")+"";
    	  String url=printUrl+"?cid="+cid+"&trxdt="+trxdt+"&csts="+csts+"&docCD="+docCD+"&usid="+usid;
		  return url;
	}
	  */

}
